// Copyright (c) 2009 Mike Haight, Andy Kipp
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


#include "stdafx.h"


Rectangle::Rectangle(float argX, float argY, float argWidth, float argHeight) : mX(argX), mY(argY), mWidth(argWidth), mHeight(argHeight)
{
}


Rectangle::Rectangle(Coordinates::Location argLocation, Coordinates::Size argSize) : mX(argLocation.X), mY(argLocation.Y), mWidth(argSize.Width), mHeight(argSize.Height)
{
}


float Rectangle::X::get(void)
{
    return mX;
}


void Rectangle::X::set(float value)
{
    mX = value;
}


float Rectangle::Y::get(void)
{
    return mY;
}


void Rectangle::Y::set(float value)
{
    mY = value;
}


float Rectangle::Width::get(void)
{
    return mWidth;
}


void Rectangle::Width::set(float value)
{
    mWidth = value;
}


float Rectangle::Height::get(void)
{
    return mHeight;
}


void Rectangle::Height::set(float value)
{
    mHeight = value;
}


float Rectangle::Top::get(void)
{
    return mY;
}


float Rectangle::Left::get(void)
{
    return mX;
}


float Rectangle::Right::get(void)
{
    return mX + mWidth;
}


float Rectangle::Bottom::get(void)
{
    return mY + mHeight;
}


bool Rectangle::IsEmpty(void)
{
    if (0 == mWidth && 0 == mHeight)
    {
        return true;
    }

    return false;
}


bool Rectangle::Contains(Location argLocation)
{
    if ((argLocation.X >= Left) && (argLocation.X <= Right) &&
        (argLocation.Y >= Top) && (argLocation.Y <= Bottom))
    {
        return true;
    }

    return false;
}


bool Rectangle::Intersects(Rectangle argRectangle)
{
    if (Bottom < argRectangle.Top) return false;
    if (Top > argRectangle.Bottom) return false;
    if (Right < argRectangle.Left) return false;
    if (Left > argRectangle.Right) return false;
    return true;
}


bool Rectangle::Equals(Object^ argObject)
{
    if (Rectangle::typeid == argObject->GetType())
    {
        Rectangle^ rectangle = (Rectangle^)argObject;
        if ((mX == rectangle->mX) && (mY == rectangle->mY) &&
            (mWidth == rectangle->mWidth) && (mHeight == rectangle->mHeight))
        {
            return true;
        }
    }

    return false;
}


String^ Rectangle::ToString(void)
{
    return String::Format("([{0}, {1}], [{2}, {3}])", mX, mY, mWidth, mHeight);
}


Coordinates::Rectangle Rectangle::Parse(String^ argText)
{
    if (nullptr == argText)
    {
        throw gcnew ArgumentNullException();
    }

    array<String^>^ elements = argText->Split(',');

    if (!elements->Length.Equals(4))
    {
        throw gcnew FormatException();
    }

    return Coordinates::Rectangle(
        float::Parse(elements[0]),
        float::Parse(elements[1]),
        float::Parse(elements[2]),
        float::Parse(elements[3]));
}